<template>
  <div class="mod-config">
    <el-form :inline="true">
      <el-form-item>
        <date-phase ref="datePhase" dateTypeRange="02" :datePhase="datePhase" ></date-phase>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getReport()" :loading="dataListLoading">查询</el-button>
        <el-button type="primary" @click="exportExcel">导出</el-button>
      </el-form-item>
    </el-form>
      <el-table :data="reportItems"  id="sale-table">
          <el-table-column fixed
            prop="datePhase"
            label="时间"
            width="120">
          </el-table-column>
        <el-table-column
          align="center"
            label="梦之队">
          <el-table-column
            prop=""
            label="新客户"
            width="80">
            <template slot-scope="scope">
             {{getDreamTeam(scope.row).newCustomerValue}}
            </template>
          </el-table-column>
          <el-table-column
            prop=""
            label="老客户"
            width="80">
            <template slot-scope="scope">
              {{getDreamTeam(scope.row).oldCustomerValue}}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column
          align="center"
          label="福来队">
          <el-table-column
            prop="newCustomerValue"
            label="新客户"
            width="80">
            <template slot-scope="scope">
              {{getFulaiTeam(scope.row).newCustomerValue}}
            </template>
          </el-table-column>
          <el-table-column
            prop=""
            label="老客户"
            width="80">
            <template slot-scope="scope">
              {{getFulaiTeam(scope.row).oldCustomerValue}}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column
          align="center"
          label="超越队">
          <el-table-column
            prop="newCustomerValue"
            label="新客户"
            width="80">
            <template slot-scope="scope">
              {{getExceedTeam(scope.row).newCustomerValue}}
            </template>
          </el-table-column>
          <el-table-column
            prop=""
            label="老客户"
            width="80">
            <template slot-scope="scope">
              {{getExceedTeam(scope.row).oldCustomerValue}}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column
          align="center"
          label="合计">
          <el-table-column
            prop="newCustomerValue"
            label="新客户"
            width="80">
          </el-table-column>
          <el-table-column
            prop="oldCustomerValue"
            label="老客户"
            width="80">
          </el-table-column>
          <el-table-column
            prop="valueTotal"
            label="总计"
            width="80">
          </el-table-column>
        </el-table-column>
        <el-table-column
          prop="aliPlatCost"
          label="阿里平台费用"
          width="100">
        </el-table-column>
        <el-table-column
          prop="carCost"
          label="直通车费用"
          width="100">
        </el-table-column>
        <el-table-column
          prop="smCost"
          label="社媒"
          width="100">
        </el-table-column>
        <el-table-column
          prop="gsCost"
          label="谷歌"
          width="100">
        </el-table-column>
        <el-table-column
          prop="adCost"
          label="广告费"
          width="100">
        </el-table-column>
        <el-table-column
          prop="acos"
          label="ACOS%"
          width="100">
        </el-table-column>
        <el-table-column
          align="center"
          label="新客户折前金额">
          <el-table-column
            prop=""
            label="梦之队"
            width="80">
            <template slot-scope="scope">
              {{getDreamTeam(scope.row).newCustomerValueBd}}
            </template>
          </el-table-column>
          <el-table-column
            prop=""
            label="福来队"
            width="80">
            <template slot-scope="scope">
              {{getFulaiTeam(scope.row).newCustomerValueBd}}
            </template>
          </el-table-column>
          <el-table-column
            prop=""
            label="超越队"
            width="80">
            <template slot-scope="scope">
              {{getExceedTeam(scope.row).newCustomerValueBd}}
            </template>
          </el-table-column>
          <el-table-column
            prop="newCustomerValueBd"
            label="合计"
            width="80">
          </el-table-column>
          <el-table-column
            prop="newCustomerDiscountRate"
            label="折扣均值%"
            width="80">
          </el-table-column>
        </el-table-column>
        <el-table-column
          align="center"
          label="老客户折前金额">
          <el-table-column
            prop="newCustomerOrderNum"
            label="梦之队"
            width="80">
            <template slot-scope="scope">
              {{getDreamTeam(scope.row).oldCustomerValueBd}}
            </template>
          </el-table-column>
          <el-table-column
            prop="oldCustomerOrderNum"
            label="福来队"
            width="80">
            <template slot-scope="scope">
              {{getFulaiTeam(scope.row).oldCustomerValueBd}}
            </template>
          </el-table-column>
          <el-table-column
            prop="oldCustomerOrderNum"
            label="超越队"
            width="80">
            <template slot-scope="scope">
              {{getExceedTeam(scope.row).oldCustomerValueBd}}
            </template>
          </el-table-column>
          <el-table-column
            prop="oldCustomerValueBd"
            label="合计"
            width="80">
          </el-table-column>
          <el-table-column
            prop="oldCustomerDiscountRate"
            label="折扣均值%"
            width="80">
          </el-table-column>
        </el-table-column>
      </el-table>
    </div>
</template>

<script>
  import DatePhase from  '@/components/sale/date-phase'
  import dingMixin from  '@/mixins/ding'
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'

  export default {
    data () {
      return {
        datePhase:{
            dateType: 2,
            value:'',
            to:''
        },
        dingId:'',
        editMode: false,
        dataListLoading: false,
        saveLoading:false,
        reportItems:[]
      }
    },
    mixins: [dingMixin],
    components: {
      DatePhase
    },
    activated () {
      //this.getDataList()

    },
    created(){
      //this.getTeamMember()
    },
    methods: {
      exportExcel(){
        this.$nextTick(function () {
          let wb = XLSX.utils.table_to_book(document.getElementById('sale-table'));
          let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
          try {
            FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), 'b2b-summary.xlsx')
          } catch (e) {
            if (typeof console !== 'undefined') console.log(e, wbout)
          }
          return wbout;
        });
      },
      getTeamData(teamName, dataItem){
        let teams = dataItem.teams.filter(x=>x.teamName==teamName);
        if(teams.length == 0){
          return {}
        }
        return teams[0]
      },
      getFulaiTeam(dataItem){
        return this.getTeamData('福来队', dataItem)
      },
      getDreamTeam(dataItem){
        return this.getTeamData('梦之队', dataItem)
      },
      getExceedTeam(dataItem){
        return this.getTeamData('超越队', dataItem)
      },
      // 获取数据列表
      getReport () {
        if(this.datePhase.value == null || this.datePhase.value == ''){
          this.$message({message: '请选择时间', type: 'error', duration: 3000})
          return
        }
        this.editMode = false
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sale/getB2bTotal'),
          method: 'get',
          params: this.$http.adornParams({
            'datePhase': this.datePhase.value,
            'dateType': this.datePhase.dateType,
            'to': this.datePhase.to,
            'dingId': this.dingId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.reportItems = data.data
          } else {
            this.reportItems = []
          }
        }).finally(()=>{
          this.dataListLoading = false
        })
      }
    }
  }
</script>
<style lang="scss">

  .el-table{
    font-size: 12px;
  }

  .edit-cell{
    width: 60px;
  }

  .el-input-number.is-without-controls .el-input__inner{
    padding: 0;
    text-align: left;
  }

</style>
